<!-- src/views/RegisterView.vue -->
<template>
  <div class="register-container">
    <el-card class="register-card">
      <template #header>
        <div class="card-header">
          <span>用户注册</span>
        </div>
      </template>

      <el-form
          ref="registerFormRef"
          :model="registerForm"
          :rules="registerRules"
          label-width="80px"
          @submit.prevent="handleRegister"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
              v-model="registerForm.username"
              placeholder="请输入用户名"
              clearable
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
              v-model="registerForm.password"
              type="password"
              placeholder="请输入密码"
              show-password
          />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              placeholder="请再次输入密码"
              show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button
              type="primary"
              @click="handleRegister"
              :loading="loading"
              style="width: 100%"
          >
            {{ loading ? '注册中...' : '注册' }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import {ref, reactive} from 'vue'
import {useRouter} from 'vue-router'
import type {FormInstance, FormRules} from 'element-plus'
import {registerApi} from '@/api/request'
import {ElMessage} from 'element-plus'

// 表单数据
const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: ''
})

// 表单验证规则
const registerRules = reactive<FormRules>({
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 3, max: 20, message: '用户名长度为3-20个字符', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, max: 20, message: '密码长度为6-20个字符', trigger: 'blur'}
  ],
  confirmPassword: [
    {required: true, message: '请再次输入密码', trigger: 'blur'},
    {validator: validatePassword, trigger: 'blur'}
  ]
})

// 密码一致性验证
function validatePassword(rule: any, value: string, callback: Function) {
  if (value !== registerForm.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

// 表单引用
const registerFormRef = ref<FormInstance>()

// 状态
const loading = ref(false)
const router = useRouter()

// 注册处理函数
const handleRegister = async () => {
  if (!registerFormRef.value) return

  await registerFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true

      try {
        // 调用注册接口
        const response = await registerApi({
          username: registerForm.username,
          password: registerForm.password
        })

        if (response.success) {
          ElMessage.success('注册成功')

          // 跳转到登录页面
          router.push('/login')
        } else {
          ElMessage.error(response.message || '注册失败')
        }
      } catch (error: any) {
        console.error('注册失败:', error)
        if (error.response && error.response.data) {
          ElMessage.error(error.response.data.message || '注册失败，请检查信息')
        } else {
          ElMessage.error('注册失败，请检查网络或稍后重试')
        }
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5;
  margin: 0;
  padding: 20px;
  box-sizing: border-box;
}

.register-card {
  width: 100%;
  max-width: 400px;
  max-height: calc(100vh - 40px);
  overflow: hidden;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-container {
    padding: 10px;
  }

  .register-card {
    max-height: calc(100vh - 20px);
  }
}

@media (max-height: 600px) {
  .register-container {
    align-items: flex-start;
    padding-top: 10px;
  }

  .register-card {
    max-height: calc(100vh - 20px);
  }
}
</style>
